//
//  SQComposeMenuView.swift
//  SwiftWeibo
//
//  Created by 苏强 on 16/11/16.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import UIKit

class SQComposeMenuView: UIVisualEffectView {
    
    
    let time = 0.3;
    
    var menuButtons = [UIButton]()
    
    override init(effect: UIVisualEffect?) {
        
        super.init(effect: UIBlurEffect(style: .extraLight))
        
        contentView.addSubview(bottomView)
        bottomView.addSubview(plusIcon)
        
        setupMenus()
        
        UIView.animate(withDuration: time) { 
            self.plusIcon.transform = CGAffineTransform(rotationAngle: CGFloat((M_PI_4)))
        }
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SQComposeMenuView.close))
        contentView .addGestureRecognizer(tapGesture)
        
        
    }
    
    
    private func setupMenus()
    {
        let menus:[NSDictionary] = [["imageName":"tabbar_compose_weibo", "title":"纯文字"],
                                 ["imageName":"tabbar_compose_photo", "title":"照片/视频"],
                                 ["imageName":"tabbar_compose_weibo", "title":"头条文章"],
                                 ["imageName":"tabbar_compose_weibo", "title":"直播"],
                                 ["imageName":"tabbar_compose_weibo", "title":"更多"]]
//                                 ["imageName":"tabbar_compose_weibo", "title":"点评"],
//                                 ["imageName":"tabbar_compose_weibo", "title":"好友圈"],
//                                 ["imageName":"tabbar_compose_weibo", "title":"音乐"],
//                                 ["imageName":"tabbar_compose_weibo", "title":"秒拍"],
//                                 ["imageName":"tabbar_compose_weibo", "title":"商品"],
//                                 ["imageName":"tabbar_compose_weibo", "title":"红包"]]
        
        for dict in menus
        {
            guard
                let imageName = dict["imageName"] as? String,
                let title = dict["title"] as? String
            else
            {
                continue
            }
            
            let btn = UIButton()
            btn.setImage(UIImage(named:imageName), for: .normal)
            btn.setTitle(title, for: .normal)
            btn.setTitleColor(UIColor.gray, for: .normal)
            btn.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
            btn.imageView?.transform = CGAffineTransform(rotationAngle: CGFloat(-M_PI_2))
            btn.titleLabel?.transform = CGAffineTransform(rotationAngle: CGFloat(-M_PI_2))
            menuButtons.append(btn)
            contentView.addSubview(btn)
            
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        effect = UIBlurEffect(style: .extraLight)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let height: CGFloat = 44.0;
        
        bottomView.frame = CGRect(x: 0, y: bounds.height - height, width: bounds.width, height: height)
        plusIcon.frame = CGRect(x: (bounds.width - height) * 0.5 , y: 0, width: height, height: height)
        
        let width = bounds.width / 3;
        
        let bottomMargin : CGFloat = 100
        
        let y = bounds.height - bottomMargin - 2 * width;
        
        for i in 0 ..< menuButtons.count
        {
            let row = CGFloat(i / 3)
            let coloumn = CGFloat(i % 3)
            
            let btn = menuButtons[i]
            btn.backgroundColor = UIColor.red
            btn.imageView?.backgroundColor = UIColor.orange
            btn.titleLabel?.backgroundColor = UIColor.blue
            
            btn.frame = CGRect(x: coloumn * width, y: row * width + y, width: width, height: width)
            btn.titleLabel?.sizeToFit()
            
            
        }
        
    }
    
    // MARK: 时间
    @objc private func close()
    {
        
        UIView.animate(withDuration: time, animations: { 
            self.plusIcon.transform = CGAffineTransform.identity
            }) { (bool) in
                self.removeFromSuperview()
        }
    }
    
    
    
    // MARK: 懒加载
    private var bottomView : UIView = {
        var view = UIView()
        view.backgroundColor = UIColor.white
        return view;
    }()
    
    
    private var plusIcon : UIImageView = {
        var imageView = UIImageView(image: UIImage(named:"tabbar_compose_background_icon_add"))
        imageView.contentMode = .center
        return imageView
    }()

}


//public enum SQMenuType{
//    case pureText
//    case media
//    case headline
//    case sign
//    case live
//    case comment
//    case friends
//    case music
//    case more
//}

class SQMenuButton: UIButton {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        return contentRect
    }
    
    
    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        return contentRect
    }
    

    override func layoutSubviews() {
        super.layoutSubviews()
    }
}

